package cn.mrlong.tools.service

import cn.mrlong.tools.bean.Note
import cn.mrlong.tools.bean.NoteDao
import cn.mrlong.tools.bean.NoteDaoImpl
import cn.mrlong.tools.service.DatabaseBasicService
import com.intellij.openapi.application.ApplicationManager
import java.sql.Connection
import java.sql.SQLException

/**
 * @author LeeWyatt
 */
class NoteServiceImpl private constructor() : NoteService {
    private val databaseBasicService: DatabaseBasicService =
            ApplicationManager.getApplication().getService(DatabaseBasicService::class.java)
    private val noteDao: NoteDao = NoteDaoImpl.Companion.instance as NoteDaoImpl

    override fun insert(ary: Array<Note?>?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.insert(conn, ary)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun insert(note: Note?): Note? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.insert(conn, note)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun delete(id: Int?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.delete(conn, id)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun update(ary: Array<Note?>?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.update(conn, ary)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun update(note: Note?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.update(conn, note)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun findById(id: Int?): Note? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.findById(conn, id)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun exchangeShowOrder(showOrder1: Int?, showOrder2: Int?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.exchangeShowOrder(conn, showOrder1, showOrder2)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun findAllByChapterId(chapterId: Int?): List<Note?>? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.findAllByChapterId(conn, chapterId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun deleteAllByChapterId(chapterId: Int?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.deleteAllByChapterId(conn, chapterId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun deleteAllByNotebookId(notebookId: Int?) {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            noteDao.deleteAllByNotebookId(conn, notebookId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
    }

    override fun getTitles(notebookTitle: String?, chapterTitle: String?): List<String?>? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.getTitles(conn, notebookTitle, chapterTitle)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun findByTitle(noteTitle: String?, chapterId: Int?): Note? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.findByTitle(conn, noteTitle, chapterId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun findByTitles(noteTile: String?, chapterTitle: String?, notebookTitle: String?): Note? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.findByTitles(conn, noteTile, chapterTitle, notebookTitle)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun getImageRecordsByNotebookId(notebookId: Int): List<String?>? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.getImageRecordsByNotebookId(conn, notebookId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun getImageRecordsByChapterId(chapterId: Int): List<String?>? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.getImageRecordsByChapterId(conn, chapterId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }

    override fun getImageRecordsByNoteId(noteId: Int): List<String?>? {
        var conn: Connection? = null
        try {
            conn = databaseBasicService.connection
            return noteDao.getImageRecordsByNoteId(conn, noteId)
        } catch (throwables: SQLException) {
            throwables.printStackTrace()
        } finally {
            databaseBasicService.closeResource(conn, null, null)
        }
        return null
    }


    companion object {
        val instance: NoteServiceImpl
            get() = ApplicationManager.getApplication().getService(NoteServiceImpl::class.java)
    }
}